/*
*Copyright © 2019 XSNOW
*四川瑞韬科技有限公司
*http://www.ratotech.com.cn
*All rights reserved.
*/
package com.icesoft.xsnow.upms.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.icesoft.xsnow.framework.api.reponses.R;
import com.icesoft.xsnow.framework.api.util.ApiAssert;
import com.icesoft.xsnow.framework.controller.BaseController;
import com.icesoft.xsnow.upms.api.po.OauthClientDetails;
import com.icesoft.xsnow.upms.core.enums.ClientEcodeEnum;
import com.icesoft.xsnow.upms.service.OauthClientDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @program: xsnow
 * @description:  前端控制器
 * @author xuefeng.gao
 * @create: 2019-05-28
 */
@RestController
@Validated
@RequestMapping(value = "oauth/client", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = {"OauthClientDetails"}, description = "相关接口")
public class OauthClientDetailsController extends BaseController {
    @Autowired
    private  OauthClientDetailsService oauthClientDetailsService;

    @ApiOperation(" 通过id查询")
    @ApiImplicitParams({ @ApiImplicitParam(name = " id", value = "id", required = true, paramType = "path") })
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) String id) {
        return success(oauthClientDetailsService.getById(id));
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R page(@RequestParam(value = "clientId", required = false) String clientId) {
        Wrapper<OauthClientDetails> queryWrapper = new LambdaQueryWrapper<OauthClientDetails>()
               .like(StrUtil.isNotEmpty(clientId),OauthClientDetails::getClientId,clientId);
        return success(oauthClientDetailsService.page(this.getPage(),queryWrapper));
    }

    @ApiOperation("新增")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('oauth:client_details:add')" )
    public R save(@RequestBody OauthClientDetails oauthClientDetails) {
        OauthClientDetails clientDetails = oauthClientDetailsService.getById(oauthClientDetails.getClientId());
        //终端是否存在
        ApiAssert.isNull(ClientEcodeEnum.CLIENT_FOUND,clientDetails);
        return success(oauthClientDetailsService.save(oauthClientDetails));
    }

    @ApiOperation("修改")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('oauth:client_details:edit')" )
    public R updateById(@RequestBody OauthClientDetails oauthClientDetails) {
        OauthClientDetails clientDetails = oauthClientDetailsService.getById(oauthClientDetails.getClientId());
        //终端是否存在
        ApiAssert.notNull(ClientEcodeEnum.CLIENT_NOT_FOUND,clientDetails);
        return success(oauthClientDetailsService.updateById(oauthClientDetails));
    }

    @ApiOperation("删除" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('oauth:client_details:del')" )
    public R removeById(@PathVariable String id) {
        return success(oauthClientDetailsService.removeById(id));
    }

}
